package com.yhm.action;

import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.yhm.barcode.createCode128;
import com.yhm.service.XlsManager;
import com.yhm.util.page.OrderPage;

@SuppressWarnings("serial")
@Component("xls")
@Scope("prototype")
public class XlsAction extends ActionSupport {
	private InputStream excelStream;
	private String downloadFileName;
	private XlsManager xlsManager;
	private OrderPage page;
	private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

	public String create_order_list_xls() throws ParseException {
		deltemp();
		if(page == null)
		page = new OrderPage();
		downloadFileName = df.format(new Date());
		excelStream = xlsManager.get_order_list_xml(page,downloadFileName);
		return "execl";
	}
	
	
	public void deltemp() {
		String file2 = createCode128.class.getResource("/").getPath()
				.replaceAll("WEB-INF/classes/", "temp");
		File file = new File(file2);
		deleteFile(file);
	}

	private void deleteFile(File file) {
		if (file.exists()) {
			if (file.isFile()) {
				file.delete();
			} else if (file.isDirectory()) {
				File[] files = file.listFiles();
				for (int i = 0; i < files.length; i++) {
					deleteFile(files[i]);
				}
			}
			file.delete();
		} else {
			System.out.println("所删除的文件不存在！\n");
		}
	}
	
	
	
	public InputStream getExcelStream() {
		return this.excelStream;
	}

	public void setExcelStream(InputStream excelStream) {
		this.excelStream = excelStream;
	}


	public XlsManager getXlsManager() {
		return xlsManager;
	}

	@Resource(name = "xlsManager")
	public void setXlsManager(XlsManager xlsManager) {
		this.xlsManager = xlsManager;
	}

	public OrderPage getPage() {
		return page;
	}

	public void setPage(OrderPage page) {
		this.page = page;
	}


	public String getDownloadFileName() {
		return downloadFileName;
	}


	public void setDownloadFileName(String downloadFileName) {
		this.downloadFileName = downloadFileName;
	}

}
